package com.person.common;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

/**
 * @author Administrator
 *
 */
public class PageParams{
    private Integer offset;
    private Integer limit;
    private String sort;
    private String order;
    private Map<String,String> params;
	public Integer getOffset() {
		return offset;
	}
	public void setOffset(Integer offset) {
		this.offset = offset;
	}
	public Integer getLimit() {
		return limit;
	}
	public void setLimit(Integer limit) {
		this.limit = limit;
	}
	
	public Integer getPage() {
		return offset;
	}
	public void setPage(Integer page) {
		this.offset = page;
	}
	public Integer getRows() {
		return limit;
	}
	public void setRows(Integer rows) {
		this.limit = rows;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	
   public Map<String, String> getParams() {
		return params;
	}
	public void setParams(Map<String, String> params) {
		this.params = params;
	}
	
public String getOrderStr()throws Exception{
	   try{
	   if(StringUtils.isBlank(sort)||StringUtils.isBlank(order))
	   return null;
	   else{
		  StringBuffer sb=new StringBuffer();
		  String []sorts=sort.split(",");
		  String []orders=order.split(",");
		  for(int i=0;i<sorts.length;i++){
			     sb.append(sorts[i]+" "+orders[i]+",");
		  }
		  return sb.toString().substring(0,sb.length()-1); 
	   }
	   }catch(Exception e){
		    throw  new  Exception("创建查询排序语句出错！");
	   }
   } 

/**
 * @param exCludeCol 不需要转换的字段
 * @return
 * @throws UtilException
 */
public String getOrderStr(String[]exCludeCol)throws Exception{
	   try{
	   if(StringUtils.isBlank(sort)||StringUtils.isBlank(order))
	   return null;
	   else{
		  StringBuffer sb=new StringBuffer();
		  String []sorts=sort.split(",");
		  String []orders=order.split(",");
		  for(int i=0;i<sorts.length;i++){
			  StringBuffer sbCol=new StringBuffer();
			  boolean flag=true;
			  //转化规则 将大写字母前加”_“线
			  if(exCludeCol!=null)
			  for(String col:exCludeCol){
				  if(sorts[i]!=null&&sorts[i].equalsIgnoreCase(col))
					  flag=false;
			  }
			  if(flag){
				   char[]chs=sorts[i].toCharArray();
				   for(int k=0;k<chs.length;k++){
					   if(chs[k]>='A'&&chs[k]<='Z') 
						   sbCol.append("_"+chs[k]);
					    else
					       sbCol.append(chs[k]);
				   }
				   sorts[i]=sbCol.toString();   
			  }
			     sb.append(sorts[i]+" "+orders[i]+",");
		  }
		  return sb.toString().substring(0,sb.length()-1); 
	   }
	   }catch(Exception e){
		    e.printStackTrace();
		    throw  new  Exception("创建查询排序语句出错！");
	   }
}
}
